//let baseUrl =process.env.NODE_ENV === 'development' ? '/jf' :'http://jf.boyaltd.cn'
//let newUrl="http://app31.test/api"
import config from "../../config";
export default {
	Get: function(url, data, success, fail, opt = {
		showLoading: false,
		sleep: 2500,
		showError: true
	}) {
		this.Request({
			url: url,
			data: data,
			method: "GET"
		}, success, opt, fail);
	},
	Post: function(url, data, success, fail = null, opt = {
		showLoading: false,
		sleep: 2500,
		showError: false
	}) {
		this.Request({
			url: url,
			data: data
		}, success, opt, fail);
	},
	NGet: function(url, data, success, fail, opt = {
		showLoading: true,
		sleep: 2500,
		showError: true,
		CheckToken: true
	}) {
		this.Request({
			url: config.apiUrl + url,
			data: data,
			method: "GET"
		}, success, opt, fail);
	},
	NPost: function(url, data, success, fail = null, opt = {
		showLoading: true,
		sleep: 2500,
		showError: true
	}) {
		 console.log("NPost->", config.apiUrl + url);
		this.Request({
			url: config.apiUrl + url,
			data: data,
		}, success, opt, fail);
	},
	ttsx_login: function() {
		let user = uni.getStorageSync("userInfo");
		console.log("ttsx_login", user);
		if (user) {
			this.NGet("/app/ttsx_login", null, function(res) {
				console.log("ttsx_login", res);
				uni.setStorageSync("ttsxAuthToken", res.data)
			}, null, {
				showLoading: false,
				showError: false
			});
		}
	},

	/**
	 * 请求封装
	 * @param {Object} res
	 * @param {Object} success
	 * @param {Object} fail
	 */
	Request: function(res, success, opt, fail) {

		var that = this;
		res.data = res.data ? res.data : {};
		let method = res.method ? res.method : "POST";
		let token = ''
		// if (res.data.token) {
		// 	token = res.data.token
		// } else {
		token = uni.getStorageSync('ttsxAuthToken') ? uni.getStorageSync('ttsxAuthToken') : "";
		// }
		let ttsxauthtoken = uni.getStorageSync("ttsxauthtoken");
		// if (opt.CheckToken) {
		// 	if (token == "") {
		// 		uni.reLaunch({
		// 			url: "/pages/login/login"
		// 		});
		// 	}
		// }
		if (method == "POST") {
			// res.data.token = token;
		}

		if (opt.showLoading) {
			uni.showLoading({
				title: "请求中..."
			})
		}

		let _url = '';
		if (res.url.indexOf("http") >= 0) {
			_url = res.url;
		} else {
			_url = config.jfUrl + res.url;
		}

		let user = uni.getStorageSync("member");
		// let userInfo = uni.getStorageSync("userInfo");
		let Bearer = uni.getStorageSync("Bearer");
		let _header = {
			"Content-Type": "application/json; charset=UTF-8",
			"Authorization": "Bearer " + Bearer,
			// "uuid": user.uuid,
			// "ttid": userInfo.id,
			"ttsxauthtoken": ttsxauthtoken
		}
		if (ttsxauthtoken) {
			_header["ttsxauthtoken"] = ttsxauthtoken;
		}
		let userInfo = uni.getStorageSync("userInfo");
		if (userInfo) {
			_header["ttid"] = userInfo.id;
		}

		if (res.header) {
			_header = res.header;
		}
		let self = this;
		// console.log("header", _header);
		// console.log("token: ", res.data);
		// console.log("request", _url, res.data);
		res.data["timespan"] = new Date().getTime();
		//删除url中的双//,防止请求失败,排除https://的情况
		//_url = _url.replace(/(?<=https:\/\/)(\/{2,})/g, '/');
		// console.log("url",_url);
		uni.request({
			url: _url,
			data: res.data,
			method: method,
			header: _header,
			dataType: 'json',
			sslVerify: false,
			success: function(res) {

				uni.hideLoading();
				switch (res.statusCode) {
					case 200:
						// console.log('res====>', res);
						switch (res.data.code) {
							case 200:
								if (success) {
									success(res.data);
								}
								break;
							case 500:
								fail(res.data);
								break;
							case 401:
								that.msg("登录过期，请重新登陆~");
								uni.removeStorageSync("Bearer");
								uni.removeStorageSync("token");
								uni.removeStorageSync("ttsxAuthToken");
								uni.removeStorageSync("userInfo")
								uni.reLaunch({
									url: "/pages/login/login"
								});
							default:

								if (opt.showError) {
									self.msg(res.data.msg);
									// success(res.data);
								}
								if (fail) {
									fail(res.data);
								}
								break;
						}
						break;
					case 500:
						if (fail) {
							fail(res.data);
						}
						// success(res.data);
						break;
					case 401:
						that.msg("登录过期，请重新登陆~");
						//uni.clearStorageSync();
						uni.removeStorageSync("Bearer");
						uni.removeStorageSync("token");
						uni.removeStorageSync("ttsxAuthToken");
						uni.removeStorageSync("userInfo")
						setTimeout(() => {
							uni.reLaunch({
								url: "/pages/login/login"
							})
						}, 1000)
						break;
					default:
						success(res.data);
						break;
				}

			},
			fail: function(e) {
				uni.hideLoading();
				console.log("request->error", _url, e)
				if (fail) fail();
			}
		})
	},
	msg(t) {
		uni.showToast({
			title: t,
			duration: 2500,
			icon: "none"
		});
	},
	login(fnc) {
		let api = config.jfUrl + "/api/public/login";
		let phone = uni.getStorageSync("userInfo").phone_number;
		let password = "123456";
		// #ifdef APP
		// api = "https://jf.boyaltd.cn/api/public/login";
		api = "https://jf.lgwisdomunion.cn/api/public/login";
		// #endif

		// #ifdef MP-WEIXIN
		// api = "https://jf.boyaltd.cn/api/public/login";
		api = "https://jf.lgwisdomunion.cn/api/public/login";
		// #endif
		uni.request({
			url: api,
			method: "POST",
			data: {
				password: password,
				phone: phone
			},
			success(res) {
				if (res.statusCode == 200) {
					if (res.data.code == 200) {
						uni.setStorageSync("token", res.data.token);
						uni.setStorageSync("jf", res.data.data);
						fnc(res.data.data);
					}
				}
			}
		})
	},
	user() {
		return uni.getStorageSync("jf");
	}


}
// #ifdef APP-PLUS
plus.push.addEventListener('click', function(msg) {
	console.log('用户点击推送消息:', msg);

	// 处理点击事件
	if (Object.keys(msg.payload).length === 0) {
		uni.navigateTo({
			url: '/pages/home/message/message'
		});
	} else {
		console.log('222222222222');
		uni.navigateTo({
			url: '/pages/news/newsDetail?id=' + msg.payload.id
		});
	}
})
// #endif